﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProDivingAdmin2salmon14
{
    /// <summary>
    /// The Class DiverList inherits from a BindingList of Diver and IDiverList.
    /// It contains constructors and functions related to the list of divers a contest uses. 
    /// </summary>
    public class DiverList : BindingList<Diver>, IDiverList
    {

        public DiverList()
        {
 
        }

        /// <summary>
        /// This constructor take a string representing the file path as an argument. It then searches through each diver loaded from text file and adds them to the diver list.
        /// This is used when loading divers from a text file. 
        /// </summary>
        /// <param name="filePath"></param>

        public DiverList(string filePath) 
        {
            foreach (var diver in FileManager.LoadDivers(filePath)) 
            {
                this.Add(diver);
            }
        }

        /// <summary>
        /// This constructer takes a diverList as argument. 
        /// It is used when adding divers through the Add/Edit function in the program.
        /// </summary>
        /// <param name="diverList"></param>

        public DiverList(List<Diver> diverList)
        {
            foreach (var diver in diverList)
            {
                this.Add(diver);
            }
        }

        /// <summary>
        /// CalculateScore takes a diver as argument and uses the diver.CalculateScore function. 
        /// </summary>
        /// <param name="diver"></param>

        public void CalculateScore(Diver diver) 
        {
            diver.CalculateScore();
        }

        /// <summary>
        /// The function CalculateAllDivers iterates through the list of divers and calls the diver function CalculateScore for each diver.
        /// </summary>

        public void CalculateAllDivers() 
        {
            foreach (var diver in this) 
            {
                diver.CalculateScore();
            }
        }

        /// <summary>
        /// The Shuffle function is used to randomize the dive order.  
        /// </summary>

        public void Shuffle()
        {
            Random rng = new Random();
            int n = this.Count;
            while (n > 1)
            {
                n--;
                int k = rng.Next(n + 1);
                Diver value = this[k];
                this[k] = this[n];
                this[n] = value;
            }
        }

        /// <summary>
        /// DisqualifyDiver takes a diver as argument and finds the diver in the diver list and then uses the Disqualify function in Diver.
        /// </summary>
        /// <param name="diver"></param>

        public void DisqualifyDiver(Diver diver)
        {
            foreach (var diverFromList in this)
            {
                if (diverFromList.ID == diver.ID)
                {
                    diverFromList.Disqualify();
                }
            }
        }

        /// <summary>
        /// The MarkDiverAsInjured function takes a diver as argument, finds that diver in the diver list and then uses the MarAsInjured function.
        /// </summary>
        /// <param name="diver"></param>

        public void MarkDiverAsInjured(Diver diver)
        {
            foreach (var diverFromList in this)
            {
                if (diverFromList.ID == diver.ID)
                {
                    diverFromList.MarkAsInjured();
                }
            }
        }

        /// <summary>
        /// The DeleteDiver functions uses the int given as parameter to use the RemoveAt function to delete the diver.
        /// </summary>
        /// <param name="diverIndx"></param>
        public void DeleteDiver(int diverIndx)
        {
            this.RemoveAt(diverIndx);
        }
    }
}
